using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

using FreeFlow.Client;
using System.Security.Permissions;

namespace FreeFlow.WinFormsControls
{
	/// <summary>
	/// A label control.
	/// </summary>
	public class LabelControl : Control, IFillable
	{
    /// <summary>
    /// Creates a new <see cref="LabelControl"/> instance.
    /// </summary>
		public LabelControl()
		{
      TabStop = false;
		}

    /// <summary>
    /// Gets the creation parameters.
    /// </summary>
    protected override CreateParams CreateParams
    {
      [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
      get
      {
        CreateParams cp = base.CreateParams;
        cp.ExStyle |= 0x20;
        return cp;
      }
    }

    /// <summary>
    /// Paints the background.
    /// </summary>
    /// <param name="pevent">Event arguments.</param>
    protected override void OnPaintBackground(PaintEventArgs pevent)
    {
      // do nothing
    }

    /// <summary>
    /// Paints the control.
    /// </summary>
    /// <param name="e">E.</param>
    protected override void OnPaint(PaintEventArgs e)
    {
      using (SolidBrush brush = new SolidBrush(ForeColor))
      {
        StringFormat format = new StringFormat();
        format.Alignment = alignment;
        format.FormatFlags = format.FormatFlags | StringFormatFlags.NoClip;
        Rectangle rect = ClientRectangle;
        rect.Offset(-2, 0);
        e.Graphics.DrawString(Text, Font, brush, rect, format); 
      }
    }

    private StringAlignment alignment;
    private string url;

    #region IFillable Members

    /// <summary>
    /// Renders the label with static layout information using the specified field.
    /// </summary>
    /// <param name="field">Field.</param>
    public void Render(FreeFlow.Client.Field field)
    {
      LabelField labelField = (LabelField)field;
      Text = labelField.Caption;
      Font = labelField.Font;
      ForeColor = labelField.FontColor;
      if (labelField.Align == Align.Right)
        alignment = StringAlignment.Far;
      else if (labelField.Align == Align.Left)
      {
        alignment = StringAlignment.Near;
        // Not sure what is going on here, but seems necessary to get all the text to appear in labels
        Width = Width + 15;
      }
      else if (labelField.Align == Align.Center)
        alignment = StringAlignment.Center;
      else
        throw new FreeFlowException("Unrecognized alignment - " + labelField.Align.ToString());
    
      // URL
      if (labelField.Url != null)
      {
        Cursor = Cursors.Hand;
        Click += new EventHandler(LabelClick);
        url = labelField.Url.ToString();
      }
    }

    /// <summary>
    /// Fills the label with values from the specified field.
    /// </summary>
    /// <param name="field">Field.</param>
    public void Fill(FreeFlow.Client.Field field)
    {
      // does nothing
    }

    /// <summary>
    /// Fills the field from the control.
    /// </summary>
    /// <param name="field">Field.</param>
    public void FillField(Field field)
    {
    }

    #endregion
    
    private void LabelClick(object sender, EventArgs e)
    {
      Process.Start(url);
    }
  }
}
